﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Framework.Verifications;
using Pixysoft.Tools;
using Pixysoft.Weblications.Sango.Environment;

namespace Pixysoft.Weblications.Sango.Formular
{
    class SectionFormular
    {
        public const int section_pagesize = 30;

        /// <summary>
        /// 每天可公关次数为8
        /// </summary>
        public const int default_trade_count = 8;

        /// <summary>
        /// 每次迁移花费的时间
        /// </summary>
        public static long default_section_move_timespan = DateTimeCoder.Instance.HourToMilliSecond(3);

        /// <summary>
        /// 随机获取目标地区列表
        /// </summary>
        /// <param name="orms"></param>
        /// <returns></returns>
        public static List<Entities.Cti_Section> randomSections(List<Entities.Cti_Section> orms, long seed, long count)
        {
            List<int> dlist = new List<int>();
            List<Entities.Cti_Section> list = new List<Pixysoft.Weblications.Sango.Entities.Cti_Section>();

            Random random = new Random((int)seed);

            for (int i = 0; i < count; i++)
            {
                int sectionIndex = -1;
                do
                {
                    sectionIndex = random.Next(orms.Count);
                } while (dlist.Contains(sectionIndex));

                dlist.Add(sectionIndex);
                list.Add(orms[sectionIndex]);
            }

            return list;
        }


        /// <summary>
        /// 取得地区最大繁荣度
        /// </summary>
        /// <returns></returns>
        public static long GetSectionMaxProsperityvalue(long gradetypecode)
        {
            switch (gradetypecode)
            {
                case SectionGradeTypecodeCollection.GRADE_01:
                    {
                        return 2940000;
                    }
                case SectionGradeTypecodeCollection.GRADE_02:
                    {
                        return 2450000;
                    }
                case SectionGradeTypecodeCollection.GRADE_03:
                    {
                        return 1225000;
                    }
                case SectionGradeTypecodeCollection.GRADE_04:
                default:
                    {
                        return 245000;
                    }

            }
        }

        /// <summary>
        /// 地区战获得的声望
        /// </summary>
        /// <param name="winCount"></param>
        /// <returns></returns>
        public static long GetSectionRvrFamevalue(int maxWinCount, int winCount)
        {
            //每个星期2场，每场最多（最大4连胜2场胜即可）50声望

            if (winCount <= 0)
                return 10;

            double famevalue = winCount * 50 / 0.5 / maxWinCount;

            return (long)Math.Round(famevalue);
        }

        internal static long GetSectionRvrBattledate()
        {
            return GlobalTimer.Instance.ToTimeSequence() + DateTimeCoder.Instance.MinuteToMilliSecond(1);
        }

        /// <summary>
        /// 获取当前地区繁荣度下，玩家的最大等级
        /// </summary>
        /// <param name="sysSection"></param>
        /// <param name="section"></param>
        /// <returns></returns>
        public static long GetPlayerMaxLevel(Entities.Sys_Section sysSection, Entities.Usr_Section section)
        {
            Entities.Sys_Sectionscale scale = SysSectionScaleHelper.SearchByTypecode(section.Scaletypecode);

            return scale.Playermaxlevel;
        }

        /// <summary>
        /// 获取当前地区国家占有率
        /// </summary>
        /// <param name="nationtypecode"></param>
        /// <param name="orm"></param>
        /// <returns></returns>
        public static long GetNationHoldValue(long nationtypecode, Entities.Cti_Section orm)
        {
            switch (nationtypecode)
            {
                case NationTypecodeCollection.CAMP_1:
                    {
                        return orm.Nation1holdvalue;
                    }
                case NationTypecodeCollection.CAMP_2:
                    {
                        return orm.Nation2holdvalue;
                    }
                case NationTypecodeCollection.CAMP_3:
                    {
                        return orm.Nation3holdvalue;
                    }
                case NationTypecodeCollection.CAMP_NONE:
                default:
                    return 0;
            }
        }

        /// <summary>
        /// 设置国家占有率
        /// </summary>
        /// <param name="nationtypecode"></param>
        /// <param name="orm"></param>
        /// <param name="value"></param>
        public static void SetNationHoldValue(long nationtypecode, Entities.Cti_Section orm, long value)
        {
            switch (nationtypecode)
            {
                case NationTypecodeCollection.CAMP_1:
                    {
                        orm.Nation1holdvalue = value; break;
                    }
                case NationTypecodeCollection.CAMP_2:
                    {
                        orm.Nation2holdvalue = value; break;
                    }
                case NationTypecodeCollection.CAMP_3:
                    {
                        orm.Nation3holdvalue = value; break;
                    }
                case NationTypecodeCollection.CAMP_NONE:
                default:
                    break;
            }
        }

        /// <summary>
        /// 银币投资获取声望
        /// </summary>
        /// <param name="citySection"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static long getInvestFamevalue(Pixysoft.Weblications.Sango.Entities.Cti_Section citySection, long value)
        {
            return (long)Math.Round(value / 1000f);
        }

        /// <summary>
        /// 投资获取繁荣度
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static long getInvestProsperityvalue(long value)
        {
            return (long)Math.Round(value / 1000f);
        }

        /// <summary>
        /// 贸易获取银币
        /// </summary>
        /// <param name="section"></param>
        /// <param name="profile"></param>
        /// <returns></returns>
        public static long getTradeToCash(Entities.Cti_Section section, Entities.Usr_Profile profile, long buildingLevel)
        {
            double cashvalue = (1500 / 6 + 273 * profile.Userlevel) / 12;

            if (section.Sectiontypecode == SectionTypecodeCollection.SECTION_01_ECONOMIC)
                cashvalue = cashvalue * 1.5;

            return (long)Math.Round(cashvalue * (1 + 0.01 * buildingLevel));
        }

        /// <summary>
        /// 贸易接受方获取的奖励
        /// </summary>
        /// <param name="section"></param>
        /// <param name="profile"></param>
        /// <returns></returns>
        public static long getTradeBonusCash(Entities.Cti_Section section, Entities.Usr_Profile profile, long buildingLevel)
        {
            double cashvalue = getTradeToCash(section, profile, buildingLevel);

            cashvalue = cashvalue / 3;

            return (long)Math.Round(cashvalue);
        }

        /// <summary>
        /// PVP是否掉落装备 目前默认否
        /// </summary>
        /// <returns></returns>
        internal static bool isDropEquipment()
        {
            return false;
        }

        /// <summary>
        /// PVP掉落装备名称
        /// </summary>
        /// <returns></returns>
        internal static long dropEquipment()
        {
            return -1;
        }
    }
}
